在os模組中使用rename()更改檔名
path = "./videos"
bname = "video"
count = 1
for fname in os.listdir(path):
s_name = os.path.join(path, fname)
name = os.path.splitext(fname)
new_fname = bame + str(count) + name[1]
count = count + 1
(s_fname為原始檔案全名,呼叫os.path.splitext()分割檔名和副檔名)
new_fname = os.path.join(path, new_fname)
print(s_fname, new_fname)
os.rename(s_fname, new_fname)
path = "./jpgs"
for root, dirs., files in os.walk(path):
print(root)
for fname in files:
print(os.path.join(root, fname)
shutil模組可以使用copytree(A,B),將參數目錄A的所有檔案複製到目錄B
path = "./figures"
shutil.rmtree(path) #rmtree()可以將參數目錄下所有檔案和子目錄都刪除掉
move(A,B)可以移動檔案或整個目錄(參數AB可以是檔案或目錄)